package com.nbpt.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;

public class AccountDBHelper extends SQLiteOpenHelper {
    private String sqlSearch = "select * from users;";
    private static String dbName = "users.db";
    private String sqlCreateTBL= "CREATE TABLE users (\n" +
            "    id       integer AUTO_INCREMENT PRIMARY KEY,\n" +
            "    username VARCHAR (20)           UNIQUE,\n" +
            "    password VARCHAR (20)           NOT NULL,\n" +
            "    gender   integer                NOT NULL,\n" +
            "    phone    VARCHAR (11)           NOT NULL\n" +
            ");";
    public AccountDBHelper(@Nullable Context context) {
        super(context,dbName, null, 1);
    }
    //查询账号
    public Cursor searchAccount(){
        SQLiteDatabase sqLite = getWritableDatabase();
        Cursor cursor = sqLite.rawQuery(sqlSearch,null);

        return cursor;
    }
    //插入数据
    public long  insertNewAccount(String username,String password,int gender,String phone){
//        String sq1="INSERT INTO users (username, password, gender,phone) VALUES ('"+username+"','"+password+"','"+gender+"','"+phone+"')";
//        try{
//            SQLiteDatabase sqLite = getWritableDatabase();
//            sqLite.execSQL(sq1);
//        }catch (SQLiteConstraintException e){
//            e.printStackTrace();
//        }
        ContentValues values=new ContentValues();
        values.put("username",username);
        values.put("password",password);
        values.put("gender",gender);
        values.put("phone",phone);
        SQLiteDatabase sqLite = getWritableDatabase();
        long ret=sqLite.insert("users",null,values);

        return ret;
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(sqlCreateTBL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}



